package com.anbinh.doctor.adapter;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.benhnhan.FragmentListPatients;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.DiseaseManifestations.Manifestations;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.PatientOptionType;
import com.anbinh.doctor.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class DeseaseManifestionsAdapter extends ArrayAdapter<Manifestations> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<Manifestations> listData;
	private ImageLoader imageLoader;
	private Context mContext;
	private int mTypeOption;
	private UserLogin userLogin;

	public DeseaseManifestionsAdapter(Context mContext, int resource,
			List<Manifestations> models, int typeOption) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;
		this.mTypeOption = typeOption;
		layoutInflater = LayoutInflater.from(mContext);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(mContext
				.getApplicationContext()));
		userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);
	}

	public class ViewHolder {
		public TextView lblName;
		public View remove;
	}

	@Override
	public Manifestations getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.lblName = (TextView) convertView
					.findViewById(R.id.lblName);
			viewHolder.remove = (View) convertView.findViewById(R.id.remove);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final Manifestations item = getItem(position);
		viewHolder.remove.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPopupMenu(item);
			}
		});

		if (mTypeOption == PatientOptionType.BIEUHIEN.getCode()) {
			viewHolder.lblName.setText(item.getDisease_manifestations());
		} else if (mTypeOption == PatientOptionType.TIEU_SU_BENH_GIA_DINH
				.getCode()) {
			viewHolder.lblName.setText(item.getFamily_medical_history());
		} else if (mTypeOption == PatientOptionType.LICH_SU_KHAM_CHUA_BENH
				.getCode()) {
			viewHolder.lblName.setText(item.getMedical_history());
		} else if (mTypeOption == PatientOptionType.LIST_NOTE.getCode()) {
			viewHolder.lblName.setText(item.getNote());
		}
		return convertView;
	}
	public void showPopupMenu(final Manifestations item) {
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				R.drawable.abs__menu_dropdown_panel_holo_light);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_menu_extaned);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);

		TextView lblXoaDc = (TextView) dialog.findViewById(R.id.lblXoaDc);
		lblXoaDc.setText("Xóa");

		lblXoaDc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onDelete(item);
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	protected void onDelete(final Manifestations item) {
		
		ServerController.getInstance(mContext).reuquestRemoveImagePatient(
				userLogin.getData().getToken(),
				FragmentListPatients.newInstance().getCurrentPatient().getId(),
				item.getId(),
				FragmentListPatients.newInstance().getCurrentDoctorId(),mTypeOption,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						listData.remove(item);
						notifyDataSetChanged();
					}

					@Override
					public void onError(int errorCode, String message) {

					}

				});
	}

}
